var app = new Vue({
    el: '#app',
    data: {
        permissions:['permission:add','permission:update','permission:del','permission:list'],//本页权限数组
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if(empty(token)){
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon:5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            },1200)

            return ;
        }
    },
    methods: {
        //权限控制
        permission: function (index) {
            return (_PERMISSIONS.indexOf(this.permissions[index]) != -1);
        },
        //添加
        add:function () {
            var that = this;
            var index = layer.open({
                type: 2,
                title: "新增",
                content: "edit.html?id=",
                fix: true,
                shadeClose: true,
                shade: false,
                area: ['70%', '70%'],
                maxmin: true, //开启最大化最小化按钮
            });
            layer.full(index);
        },
        //修改
        update:function () {
            var that = this;
            var list = $('#menuTable').bootstrapTreeTable('getSelections');
            if (list.length <= 0 || list.length > 1) {
                layer.msg('请选中一条数据', {time: 1500, icon: 5});
                return;
            }
            list = list[0];
            var index = layer.open({
                type: 2,
                title: "修改",
                content: "edit.html?id="+list.id,
                fix: true,
                shadeClose: true,
                shade: false,
                area: ['70%', '70%'],
                maxmin: true, //开启最大化最小化按钮
            });
            layer.full(index);
        },
        //删除
        del:function (index) {
            var that = this;
            var list = $('#menuTable').bootstrapTreeTable('getSelections');
            if (list.length <= 0 || list.length > 1) {
                layer.msg('请选中一条数据', {time: 1500, icon: 5});
                return;
            }

            var confirm = layer.confirm('确定删除选中权限？', {
                btn: ['确定', '取消'], //按钮
                shadeClose: false,
            }, function () {
                Put(that,_HOST+_API.admin_sysPermission_delete+list[0].id,{},true,that.permissions[index])
                    .then(function (response) {
                        // 响应成功回调
                        console.log(response)
                        layer.close(confirm);
                        if(response){
                            if(response.success){
                                layer.msg(response.msg, {time: 1500, icon:1});
                                setTimeout(function () {
                                    window.location.reload(); //刷新页面
                                },1200)
                            }else {
                                layer.msg(response.msg, {time: 1500, icon:5});
                            }
                        }
                    })
            });

        },
        refresh:function (index) {
            window.location.reload(); //刷新页面
        }
    },
})

$(function () {
    var treeTable = $('#menuTable').bootstrapTreeTable({
        parentCode : 'pid',// 用于设置父子关系
        type: 'get',                   // 请求方式（*）
        url: _HOST+_API.admin_sysPermission_list+"0",            // 请求后台的URL（*）
        ajaxParams : {},// 请求数据的ajax的data属性
        headers: {
            'Authorization':sessionStorageUtils.getJson(storageKey.sessionStorageKey.token),
            'permCoding':'permission:list'
        },
        toolbar: "#toolbar",      //顶部工具条
        expandColumn : 2,              // 在哪一列上面显示展开按钮
        expandFirst : false, // 是否默认第一级展开--expandAll为false时生效
        columns: [
            {
                field: 'selectItem',
                radio: true
            },
            {
                field: 'id',
                title: 'ID',
                align: 'center',
                valign: 'middle',
                width: '60px'
            },
            {
                field: 'name',
                title: '权限名称',
                align: 'center',
                valign: 'middle'
            },
            {
                field: 'icon',
                title: '图标',
                align: 'center',
                valign: 'middle',
                width: '64px',
                formatter: function (value, index) {
                    return value == null ? '' : '<i class="' + value + ' fa-lg"></i>';
                }
            },
            {
                field: 'type',
                title: '类型',
                align: 'center',
                valign: 'middle',
                width: '64px',
                formatter: function (value, index) {
                    //0、模块，1 、目录，2、菜单，3、按钮'
                    if (value=== 0) {
                        return '<span class="label label-primary">模块</span>';
                    }
                    if (value === 1) {
                        return '<span class="label label-success">目录</span>';
                    }
                    if (value === 2) {
                        return '<span class="label label-warning">菜单</span>';
                    }
                    if (value === 3) {
                        return '<span class="label label-info">按钮</span>';
                    }
                    return '-';
                }
            },
            {
                field: 'sort',
                title: '排序号',
                align: 'center',
                valign: 'middle',
                width: '64px',
            },
            {
                field: 'url',
                title: '权限URL',
                align: 'center',
                valign: 'middle'
            },
            {
                field: 'permCode',
                title: '权限编码',
                align: 'center',
                valign: 'middle'
            },
            {
                field: 'status',
                title: '状态',
                align: 'center',
                valign: 'middle',
                width: '64px',
                formatter: function (value, index) {
                    if (value == 0) {
                        return '<span class="label label-danger">禁用</span>';
                    } else if (value == 1) {
                        return '<span class="label label-primary">启用</span>';
                    }
                    return "<span>其他</span>";
                }
            },
            {
                field: 'gmtCreate',
                title: '创建时间',
                align: 'center',
                formatter: function (value, index) {
                    return formatDateTime(value);
                }
            }
        ]
    });
});